//
//  UITextView+Done.m
//
//  Created by apple on 2019/5/23.
//  Copyright © 2019 ds. All rights reserved.
//

#import "UITextView+Done.h"
#import <objc/runtime.h>

static char *kAction;
static char *kLimit;
static char *kTargetKey;

static UILabel *_placeholderLabel;

@implementation UITextView (Done)

+ (void)load{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        swizzleSelector(self, @selector(setFont:), @selector(setFont_ds:));
    });
}

static inline void swizzleSelector(Class clazz,SEL originalSelector , SEL swizzledSelector) {
    
    Class class = clazz ;
    
    Method originalMethod = class_getInstanceMethod(class, originalSelector);
    Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);
    
    
    BOOL didAddMethodInit = class_addMethod(class, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod));
    
    if (didAddMethodInit) {
        class_addMethod(class, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));
    }else{
        method_exchangeImplementations(originalMethod, swizzledMethod);
    }
}



- (void)setFont_ds:(UIFont *)font{
    [self setFont_ds:font];
    UILabel *label = [self viewWithTag:self.tag + 10000];
    if ([label isKindOfClass:[UILabel class]]) {
        _placeholderLabel = label;
    }
    if (_placeholderLabel) {
        _placeholderLabel.font = font;
        [_placeholderLabel sizeToFit];
        CGRect frame = _placeholderLabel.frame;
        _placeholderLabel.font = [UIFont systemFontOfSize:12];
        frame.origin.x = 4;
        _placeholderLabel.frame = frame;
    }
}

- (void)setPlaceholder:(NSString *)placeholder{
//    if (_placeholderLabel) {
//        [_placeholderLabel removeFromSuperview];
//    }
    
    UILabel *label = [self viewWithTag:self.tag + 10000];
    if ([label isKindOfClass:[UILabel class]]) {
        [label removeFromSuperview];
    }
    
    UILabel *placeholderLabel = [[UILabel alloc] init];
    placeholderLabel.text = placeholder;
    placeholderLabel.font = self.font?self.font:Font(15);
    placeholderLabel.textColor = NineColor;
    placeholderLabel.tag = self.tag + 10000;
    [self addSubview:placeholderLabel];
    placeholderLabel.numberOfLines = 1;
    [placeholderLabel sizeToFit];
    CGRect frame = CGRectMake(self.textContainerInset.left, self.textContainerInset.top, self.mj_w > 0 ? (self.mj_w - self.textContainerInset.left - self.textContainerInset.right) : (KScreen_W - self.textContainerInset.left - self.textContainerInset.right), placeholderLabel.frame.size.width > (self.mj_w > 0 ? (self.mj_w - self.textContainerInset.left - self.textContainerInset.right) : (KScreen_W - self.textContainerInset.left - self.textContainerInset.right)) ? placeholderLabel.frame.size.height * 2 : placeholderLabel.frame.size.height);
    frame.origin.x = self.textContainerInset.left;
    frame.origin.y = self.textContainerInset.top;
    placeholderLabel.frame = frame;
    placeholderLabel.numberOfLines = 0;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textViewTextDidChange) name: UITextViewTextDidChangeNotification object:nil];
//    self.textContainerInset = UIEdgeInsetsZero;
    if (self.text.length > 0) {
        placeholderLabel.hidden = YES;
    }
    
    _placeholderLabel = placeholderLabel;
}

- (NSString *)placeholder{
    NSString *str = @"";
    UILabel *label = [self viewWithTag:self.tag + 10000];
    if ([label isKindOfClass:[UILabel class]]) {
        str = label.text;
    }
    return str;
    
//    return objc_getAssociatedObject(self, @selector(placeholder));
}

/**
 *  限制字符提示
 *
 *  @param target   action的Target
 *  @param action   限制执行事件
 *  @param limitMax 限制数量
 */
- (void)addTarget:(id)target action:(SEL)action limitMax:(NSInteger)limitMax{
    
    NSString *limitStr = [NSString stringWithFormat:@"%zd",limitMax];
    objc_setAssociatedObject(self, &kLimit, limitStr, OBJC_ASSOCIATION_COPY);
    objc_setAssociatedObject(self, &kTargetKey, target, OBJC_ASSOCIATION_ASSIGN);
    objc_setAssociatedObject(self, &kAction, NSStringFromSelector(action), OBJC_ASSOCIATION_COPY);
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textViewTextDidChange) name: UITextViewTextDidChangeNotification object:nil];
}

/**
 *  输入内容限制
 */
- (void)textViewTextDidChange{
    UILabel *label = [self viewWithTag:self.tag + 10000];
    if ([label isKindOfClass:[UILabel class]]) {
        _placeholderLabel = label;
    }
    if (_placeholderLabel) {
        _placeholderLabel.hidden = self.text.length>0?YES:NO;
    }
}

- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    _placeholderLabel = nil;
}
- (void)addDoenButton{
    UIView *topView1 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KScreen_W, 40)];
    topView1.backgroundColor = [UIColor colorWithRed:248/255.0 green:248/255.0 blue:248/255.0 alpha:1];
    UIButton *disMissBtn1 = [UIButton buttonWithType:UIButtonTypeCustom];
    disMissBtn1.frame = CGRectMake(KScreen_W-120, 0, 100, 40);
    [disMissBtn1 setTitle:@"完成" forState:UIControlStateNormal];
    [disMissBtn1 setTitleColor:[UIColor colorWithHexCode:@"#333333"] forState:UIControlStateNormal];
    [disMissBtn1.titleLabel setFont:Font(13)];
    disMissBtn1.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    [disMissBtn1 addTarget:self action:@selector(dismissKeyBoard) forControlEvents:UIControlEventTouchUpInside];
    [topView1 addSubview:disMissBtn1];
    [self setInputAccessoryView:topView1];
}

- (void)dismissKeyBoard{
    [[[UIApplication sharedApplication] keyWindow] endEditing:YES];
}



@end
